package by.gravity.drive_track.provider;

import java.util.List;

import android.content.Context;
import by.gravity.common.task.CommonGsonAsyncTask;
import by.gravity.common.utils.GetSystemService;
import by.gravity.drive_track.api.GoogleAPI;
import by.gravity.drive_track.fragments.RouteFragment.SELECTED_MODE;
import by.gravity.drive_track.model.STATUS;
import by.gravity.drive_track.model.places.Places;
import by.gravity.drive_track.provider.listeners.OnDirectionCompleteListener;
import by.gravity.drive_track.provider.listeners.OnPlaceSearchCompleteListener;
import by.gravity.drive_track.task.DirectionTask;

import com.google.android.gms.maps.model.LatLng;

public class DriveNTrackProvider {

	public static final String DRIVE_N_TRACK_PROVIDER = "DRIVE_N_TRACK_PROVIDER";

	private Context mContext;

	public DriveNTrackProvider(Context context) {
		this.mContext = context;
	}

	public static DriveNTrackProvider get(Context context) {
		return (DriveNTrackProvider) GetSystemService.get(context, DRIVE_N_TRACK_PROVIDER);
	}

	public void getDirection(final List<LatLng> pointList, SELECTED_MODE mode,
			final OnDirectionCompleteListener onCompleteListener) {
		if (onCompleteListener == null) {
			return;
		}

		new DirectionTask(mContext) {

			@Override
			public void onComplete(List<LatLng> result, List<Integer> wayPointOrder) {
				onCompleteListener.onSuccess(result, wayPointOrder);
			}

			@Override
			public void onError() {
				onCompleteListener.onError();
			}

		}.execute(GoogleAPI.getDirectionRequest(pointList, mode));
	}

	public void searchPlace(final String name, final OnPlaceSearchCompleteListener onCompleteListener) {
		if (onCompleteListener == null) {
			return;
		}
		new CommonGsonAsyncTask<Places>(mContext) {

			@Override
			protected void onSuccess(Places result) {
				if (result.getStatus() == null) {
					return;
				}

				if (result.getStatus() == STATUS.OK) {
					onCompleteListener.onSuccess(result);
				} else {
					onCompleteListener.onError(result.getStatus());
				}

			}
		}.execute(GoogleAPI.getSearchPlacesRequest(name));
	}
}
